//
//  ThemeCGColorPicker.swift
//  HeadlineNews
//
//  Created by han on 2019/7/24.
//  Copyright © 2019 han. All rights reserved.
//

import UIKit

final class ThemeCGColorPicker: ThemePicker {

    convenience init(keyPath: String) {
        self.init(v: { ThemeManager.color(for: keyPath)?.cgColor })
    }
    
    convenience init(keyPath: String, map: @escaping (Any?) -> CGColor?) {
        self.init(v: { map(ThemeManager.value(for: keyPath)) })
    }
    
    convenience init(colors: String...) {
        self.init(v: { ThemeManager.colorElement(for: colors) })
    }
    
}

extension ThemeCGColorPicker: ExpressibleByArrayLiteral {
    convenience init(arrayLiteral elements: String...) {
        self.init(v: { ThemeManager.colorElement(for: elements)?.cgColor })
    }
}

extension ThemeCGColorPicker: ExpressibleByStringLiteral {
    convenience init(stringLiteral value: String ) {
        self.init(keyPath: value)
    }
    
    convenience init(extendedGraphemeClusterLiteral value: String) {
        self.init(keyPath: value)
    }
}

extension ThemeCGColorPicker: ExpressibleByUnicodeScalarLiteral {
    convenience init(unicodeScalarLiteral value: String) {
        self.init(keyPath: value)
    }
}

extension ThemeCGColorPicker {
    class func pickerWithKeyPath(_ keyPath: String) -> ThemeCGColorPicker {
        return ThemeCGColorPicker(keyPath: keyPath)
    }
    
    class func pickerWithKeyPath(_ keyPath: String, map:@escaping (Any?) -> CGColor?) -> ThemeCGColorPicker {
        return ThemeCGColorPicker(v: { map(ThemeManager.value(for: keyPath))})
    }
    
    class func pickerWithColors(_ colors: [String]) -> ThemeCGColorPicker{
        return ThemeCGColorPicker(v: { ThemeManager.colorElement(for: colors)?.cgColor })
    }
}
